package com.ilink.groundservice.controller.AiX;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.VO.AiX.AiXScenePhaseSectionDTO;
import com.ilink.groundservice.entity.VO.AiX.AiXScenePhaseOpusResponse;
import com.ilink.groundservice.entity.VO.AiX.AiXScenePhaseTaskDTO;
import com.ilink.groundservice.entity.VO.AiX.AiXSceneResponse;
import com.ilink.groundservice.entity.VO.AiX.AiXSceneSubPhaseOpusResponse;
import com.ilink.groundservice.service.AiX.AiXOpusService;
import com.ilink.ilinkapi.dto.ground.AiX.AiXAreaFilterRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXCoursesRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseOpusInsertRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXPhaseOpusScoreRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXScenePhaseOpusGroundRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXSubPhaseOpusInsertRequest;
import com.ilink.ilinkapi.dto.ground.AiX.AiXSubPhaseOpusScoreRequest;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import com.ilink.ilinkcommon.utils.UserContext;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RequestMapping("/ground/aix/opus")
@RestController
@RequiredArgsConstructor
public class AiXOpusController {
  private final AiXOpusService aiXOpusService;

  @MethodLog("查询阶段已提交作品")
  @GetMapping("/get_phase_opus")
  public R getPhaseOpus(@RequestParam Long phaseId, @RequestParam Long beginEndId) {
    String userId = UserContext.getUserId();
    try {
      AiXScenePhaseOpusResponse response = aiXOpusService.getPhaseOpus(phaseId, beginEndId, userId);
      if (response == null) {
        return R.ok("当前用户还没有提交作品");
      }
      return R.ok("查询阶段作品成功", response);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("提交阶段作品")
  @PostMapping("/submit_phase_opus")
  public R submitPhaseOpus(AiXPhaseOpusInsertRequest request) {
    String userId = UserContext.getUserId();
    try {
      R result = aiXOpusService.submitPhaseOpus(request, userId);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("删除阶段作品")
  @PostMapping("/delete_phase_opus")
  public R deletePhaseOpus(@RequestParam Long phaseId, @RequestParam Long beginEndId) {
    String userId = UserContext.getUserId();
    try {
      R result = aiXOpusService.deletePhaseOpus(phaseId, beginEndId, userId);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }


  @MethodLog("查询子阶段已提交作品")
  @GetMapping("/get_sub_phase_opus")
  public R getSubPhaseOpus(@RequestParam Long subPhaseId, @RequestParam Long beginEndId) {
    String userId = UserContext.getUserId();
    try {
      AiXSceneSubPhaseOpusResponse response = aiXOpusService.getSubPhaseOpus(subPhaseId, beginEndId, userId);
      if (response == null) {
        return R.ok("当前用户还没有提交作品");
      }
      return R.ok("查询子阶段作品成功", response);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }


  @MethodLog("提交子阶段作品")
  @PostMapping("/submit_sub_phase_opus")
  public R submitSubPhaseOpus(AiXSubPhaseOpusInsertRequest request) {
    String userId = UserContext.getUserId();
    try {
      R result = aiXOpusService.submitSubPhaseOpus(request, userId);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }


  @MethodLog("删除子阶段作品")
  @PostMapping("/delete_sub_phase_opus")
  public R deleteSubPhaseOpus(@RequestParam Long subPhaseId, @RequestParam Long beginEndId) {
    String userId = UserContext.getUserId();
    try {
      R result = aiXOpusService.deleteSubPhaseOpus(subPhaseId, beginEndId, userId);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取阶段所有学生作品")
  @GetMapping("/get_all_phase_opus")
  public R getAllPhaseOpus(@RequestParam Long phaseId, @RequestParam Long beginEndId) {
    try {
      List<AiXScenePhaseOpusResponse> result = aiXOpusService.getAllPhaseOpus(phaseId, beginEndId);
      return R.ok("查询阶段所有学生作品成功", result);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取子阶段所有学生作品")
  @GetMapping("/get_all_sub_phase_opus")
  public R getAllSubPhaseOpus(@RequestParam Long subPhaseId, @RequestParam Long beginEndId) {
    try {
      List<AiXSceneSubPhaseOpusResponse> result = aiXOpusService.getAllSubPhaseOpus(subPhaseId, beginEndId);
      return R.ok("查询子阶段所有学生作品成功", result);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("教师为阶段作品打分")
  @PostMapping("/score_phase_opus")
  public R scorePhaseOpus(@RequestBody AiXPhaseOpusScoreRequest request) {
    try {
      R result = aiXOpusService.scorePhaseOpus(request);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("教师为子阶段作品打分")
  @PostMapping("/score_sub_phase_opus")
  public R scoreSubPhaseOpus(@RequestBody AiXSubPhaseOpusScoreRequest request) {
    try {
      R result = aiXOpusService.scoreSubPhaseOpus(request);
      return result;
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @MethodLog("获取作品广场")
  @GetMapping("/get_opus_ground")
  public R getOpusGround(AiXAreaFilterRequest request) {
    try {
      IPage<AiXScenePhaseOpusResponse> result = aiXOpusService.getOpusGround(request);
      if (result != null) {
        return R.ok("分页返回课程元数据", result);
      } else {
        return R.error("查询课程数据失败");
      }
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }

  @MethodLog("出品人获取作品广场")
  @GetMapping("/get_opus_ground_for_author")
  public R getOpusGroundForAuthor(AiXScenePhaseOpusGroundRequest request) {
    try {
      IPage<AiXScenePhaseOpusResponse> result = aiXOpusService.getOpusGroundForAuthor(request);
      if (result != null) {
        return R.ok("分页返回课程元数据", result);
      } else {
        return R.error("查询课程数据失败");
      }
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }
  @MethodLog("获取出品人对应的微场景")
  @GetMapping("/get_author_tags")
  public R getCourseTags() {
    String userId = UserContext.getUserId();
    try {
      List<String> tags = aiXOpusService.getAuthorTags(userId);
      return R.ok("出品人对应的微场景", tags);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }
  @MethodLog("获取微场景对应的阶段")
  @GetMapping("/get_course_sections")
  public R getCourseSections(@RequestParam Long sceneId) {
    try {
      List<AiXScenePhaseSectionDTO> sections = aiXOpusService.getCourseSections(sceneId);
      return R.ok("微场景对应的阶段", sections);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }
  @MethodLog("获取阶段对应的任务")
  @GetMapping("/get_course_tasks")
  public R getCourseTasks(Long phaseId) {
    try {
      List<AiXScenePhaseTaskDTO> tasks = aiXOpusService.getCourseTasks(phaseId);
      return R.ok("出品人对应的微场景", tasks);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }
  @MethodLog("获取出品人对应的微场景")
  @GetMapping("/get_courses")
  public R getCourses(AiXAreaFilterRequest request) {
    String userId = UserContext.getUserId();
    try {
      IPage<AiXSceneResponse> courses = aiXOpusService.getCourses(request, userId);
      return R.ok("出品人对应的微场景", courses);
    } catch (Exception e) {
      log.error(e.getMessage());
      return R.error(e.getMessage());
    }
  }
}

